package com.hub.transform;

import com.hub.entity.MySensor;
import com.hub.entity.SensorReading;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class TransformConnectFromCustom {

    public static void main(String[] args) throws Exception {

        // 环境准备
        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();

        // 添加数据源：自定义数据源
        DataStreamSource<SensorReading> dataStreamSource = executionEnvironment.addSource(new MySensor());


        // 分组
        KeyedStream<SensorReading, String> sensorReadingStringKeyedStream1 = dataStreamSource.keyBy(SensorReading::getId);


        SingleOutputStreamOperator<Double> map = dataStreamSource
                                                        .keyBy(SensorReading::getId).map(item -> item.getTemplate())
                                                        .connect(sensorReadingStringKeyedStream1)
                                                        .map(new CoMapFunction<Double, SensorReading, Double>() {

                                                            @Override
                                                            public Double map1(Double aDouble) throws Exception {
                                                                return aDouble;
                                                            }

                                                            @Override
                                                            public Double map2(SensorReading sensorReading) throws Exception {
                                                                return sensorReading.getTemplate();
                                                            }
                                                        });


        // 打印任务
        map.print("connect_from_custom");

        // 执行
        executionEnvironment.execute();


    }
}
